package domain

import (
	"AcsGo/internal/util"
	"fmt"
)

// 8520240623    180346AO|AA|AD|XO261|CNsfsxbjjhj|XK12|
type ReaderInfoRequest85 struct {
	Code             util.Field
	TransactionDate  util.Field
	InstitutionId    util.FieldPrefix
	PatronIdentifier util.FieldPrefix
	PatronPassword   util.FieldPrefix
	IdCardNo         util.FieldPrefix
	Operator         util.FieldPrefix
	OperationType    util.FieldPrefix
}

func HandleReaderInfoRequest85(str string) (*ReaderInfoRequest85, error) {
	if str == "" {
		return nil, fmt.Errorf("str is empty")
	}
	util.Code.Value = "85"
	var query = ReaderInfoRequest85{
		Code:             util.Code,
		TransactionDate:  util.TransactionDate,
		InstitutionId:    util.InstitutionId,
		PatronIdentifier: util.PatronIdentifier,
		PatronPassword:   util.PatronPassword,
		IdCardNo:         util.IdCardNo,
		Operator:         util.Operator,
		OperationType:    util.OperationType,
	}

	length := query.Code.Length + query.TransactionDate.Length
	if len(str) < length {
		return nil, fmt.Errorf("str is too short")
	}

	strTemp := query.Code.GetValue(str)
	strTemp = query.TransactionDate.GetValue(strTemp)
	strTemp = query.InstitutionId.GetValue(strTemp)
	strTemp = query.PatronIdentifier.GetValue(strTemp)
	strTemp = query.PatronPassword.GetValue(strTemp)
	strTemp = query.IdCardNo.GetValue(strTemp)
	strTemp = query.Operator.GetValue(strTemp)
	_ = query.OperationType.GetValue(strTemp)
	return &query, nil
}

type ReaderInfoResponse85 struct {
	Code             util.Field
	TransactionDate  util.Field
	InstitutionId    util.FieldPrefix
	PatronIdentifier util.FieldPrefix
	OK               util.FieldPrefix
	ScreenMessage    util.FieldPrefix
	PrintLine        util.FieldPrefix
}

func (q ReaderInfoResponse85) StringFormat() (string, error) {
	sb, err := util.ConcatenateFields(q)
	if err != nil {
		return "", err
	}
	return sb, nil
}

func InitReaderInfoResponse85(ok, rdbm string) *ReaderInfoResponse85 {
	// 8620240623    180339AO|AA|OK0|AF该身份证号和密码的限定条件下查询不到读者记录!|AG该身份证号和密码的限定条

	util.Code.Value = "86"
	util.OK.Value = ok
	var message string
	if ok == "0" {
		message = "该身份证号和密码的限定条件下查询不到读者记录!"
	}
	if ok == "1" {
		message = "该身份证号查询到读者记录!"
	}
	util.ScreenMessage.Value = message
	util.PrintLine.Value = message
	util.TransactionDate.Value = util.GetCurrentTimeInFormat()
	util.PatronIdentifier.Value = rdbm
	var queryResponse = ReaderInfoResponse85{
		Code:             util.Code,
		TransactionDate:  util.TransactionDate,
		InstitutionId:    util.InstitutionId,
		PatronIdentifier: util.PatronIdentifier,
		OK:               util.OK,
		ScreenMessage:    util.ScreenMessage,
		PrintLine:        util.PrintLine,
	}

	return &queryResponse
}
